// login.js
import WxValidate from '../../utils/WxValidate'
import Countdown from '../../utils/countdown'
import { alert } from '../../utils/util'
import { getCode, login, learnNumLogin } from '../../utils/apis'
var initCount = 60
const app = getApp()

Page({
  data: {
    codeLabel: '获取验证码',
    phone: '',
    isNum: false //是否验证码登录
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.callback = options.callback || 'callback'
    if (!wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: '/pages/login/shouquan'
      })
    }
    this.countdown = new Countdown(this, 'count')
    this.initValidate()
  },

  onUnload: function() {
    // 页面关闭
    if (this.countdown) {
      this.countdown.stop()
    }
  },

  initValidate() {
    if (this.data.isNum) {
      this.validate = new WxValidate({
        phone: {
          required: true,
          tel: true
        },
        code: {
          required: true
        }
      }, {
        phone: {
          required: '请输入手机号',
          tel: '请输入有效手机号码'
        },
        code: {
          required: '请输入验证码'
        }
      })
    } else {
      this.validate = new WxValidate({
        learnNum: {
          required: true
        },
        password: {
          required: true
        }
      }, {
        learnNum: {
          required: '请输入学号',
        },
        password: {
          required: '请输入密码',
        }
      })
    }

  },
  onChangeLogin() {
    this.setData({
      isNum: !this.data.isNum
    })
    this.initValidate()
  },
  onInputPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 获取验证码
  onGetCode(e) {
    if (!wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: '/pages/login/shouquan'
      })
      return
    }
    var that = this
    var { phone, count } = this.data
    if (count > 0) {
      return
    }
    if (!/^1[345678]\d{9}$/.test(phone)) {
      return alert('请输入有效手机号码')
    }
    that.setData({
      count: initCount
    })
    that.countdown.start()
    getCode({
        phone
      })
      .then(data => {
        that.setData({
          codeLabel: '重新获取验证码'
        })
      })
      .catch(err => {
        that.countdown.stop()
        that.setData({
          count: 0
        })
      })
  },
  formSubmit(e) {
    var that = this
    if (!wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: '/pages/login/shouquan'
      })
      return
    }
    var { loading } = this.data
    if (loading) {
      return
    }
    if (!this.validate.checkForm(e)) {
      const error = this.validate.errorList[0]
      return alert(error.msg)
    }
    this.setData({
      loading: true
    })
    var { openId } = wx.getStorageSync('user')
    if (this.data.isNum) {
      var { phone, code } = e.detail.value
      login({
          phone,
          code,
          openId,
          avatar: wx.getStorageSync('userInfo').avatarUrl || '',
          nickname: wx.getStorageSync('userInfo').nickName || '',
          attach: wx.getStorageSync('userInfo')
        })
        .then(result => {
          that.setData({
            loading: false
          })

          app.globalData.loginInfo = result
          console.log()

          wx.setStorageSync('loginInfo', result)
          wx.navigateBack()
        })
        .catch(err => {
          that.setData({
            loading: false
          })
        })
    } else {
      var { learnNum, password } = e.detail.value
      learnNumLogin({
          learnNum,
          password,
          openId,
          avatar: wx.getStorageSync('userInfo').avatarUrl || '',
          nickname: wx.getStorageSync('userInfo').nickName || '',
          attach: wx.getStorageSync('userInfo')
        })
        .then(result => {
          that.setData({
            loading: false
          })

          app.globalData.loginInfo = result
          wx.setStorageSync('loginInfo', result)
          wx.navigateBack()
        })
        .catch(err => {
          that.setData({
            loading: false
          })
        })

    }
  }
})